package com.luom.fsp.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.luom.fsp.annotation.AuthCheck;
import com.luom.fsp.common.BaseResponse;
import com.luom.fsp.common.DeleteRequest;
import com.luom.fsp.common.ErrorCode;
import com.luom.fsp.common.ResultUtils;
import com.luom.fsp.constant.UserConstant;
import com.luom.fsp.exception.BusinessException;
import com.luom.fsp.exception.ThrowUtils;
import com.luom.fsp.model.dto.productSKU.ProductSKUAddRequest;
import com.luom.fsp.model.dto.productSKU.ProductSKUUpdateRequest;
import com.luom.fsp.model.dto.productSKU.ProductSKUQueryRequest;
import com.luom.fsp.model.entity.ProductSKU;
import com.luom.fsp.model.entity.User;
import com.luom.fsp.model.vo.ProductSKUVO;
import com.luom.fsp.service.ProductSKUService;
import com.luom.fsp.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 商品规格接口
 *
*/
@RestController
@RequestMapping("/productSKU")
@Slf4j
public class ProductSKUController {

    @Resource
    private ProductSKUService productSKUService;

    // region 增删改查

    /**
     * 创建商品规格
     *
     * @param productSKUAddRequest
     * @return
     */
    @PostMapping("/add")
    public BaseResponse<Long> addProductSKU(@RequestBody ProductSKUAddRequest productSKUAddRequest) {
        ThrowUtils.throwIf(productSKUAddRequest == null, ErrorCode.PARAMS_ERROR);
        ProductSKU productSKU = new ProductSKU();
        BeanUtils.copyProperties(productSKUAddRequest, productSKU);
        // 数据校验
        productSKUService.validProductSKU(productSKU, true);
        // 不能超过10个
        Long productId = productSKU.getProductId();
        List<ProductSKU> list = productSKUService.list(new QueryWrapper<ProductSKU>().eq("productId", productId));
        ThrowUtils.throwIf(list.size() > 10, ErrorCode.PARAMS_ERROR, "商品规格数量不能超过10个");
        // 写入数据库
        boolean result = productSKUService.save(productSKU);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        // 返回新写入的数据 id
        long newProductSKUId = productSKU.getId();
        return ResultUtils.success(newProductSKUId);
    }

    /**
     * 删除商品规格
     *
     * @param deleteRequest
     * @param request
     * @return
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteProductSKU(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long id = deleteRequest.getId();
        // 判断是否存在
        ProductSKU oldProductSKU = productSKUService.getById(id);
        ThrowUtils.throwIf(oldProductSKU == null, ErrorCode.NOT_FOUND_ERROR);
        // 操作数据库
        boolean result = productSKUService.removeById(id);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    /**
     * 更新商品规格（仅管理员可用）
     *
     * @param productSKUUpdateRequest
     * @return
     */
    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateProductSKU(@RequestBody ProductSKUUpdateRequest productSKUUpdateRequest) {
        if (productSKUUpdateRequest == null || productSKUUpdateRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        ProductSKU productSKU = new ProductSKU();
        BeanUtils.copyProperties(productSKUUpdateRequest, productSKU);
        // 数据校验
        productSKUService.validProductSKU(productSKU, false);
        // 判断是否存在
        long id = productSKUUpdateRequest.getId();
        ProductSKU oldProductSKU = productSKUService.getById(id);
        ThrowUtils.throwIf(oldProductSKU == null, ErrorCode.NOT_FOUND_ERROR);
        // 操作数据库
        boolean result = productSKUService.updateById(productSKU);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    /**
     * 根据 id 获取商品规格（封装类）
     *
     * @param id
     * @return
     */
    @GetMapping("/get/vo")
    public BaseResponse<ProductSKUVO> getProductSKUVOById(long id, HttpServletRequest request) {
        ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
        // 查询数据库
        ProductSKU productSKU = productSKUService.getById(id);
        ThrowUtils.throwIf(productSKU == null, ErrorCode.NOT_FOUND_ERROR);
        // 获取封装类
        return ResultUtils.success(productSKUService.getProductSKUVO(productSKU, request));
    }

    /**
     * 分页获取商品规格列表（仅管理员可用）
     *
     * @param productSKUQueryRequest
     * @return
     */
    @PostMapping("/list/page")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<ProductSKU>> listProductSKUByPage(@RequestBody ProductSKUQueryRequest productSKUQueryRequest) {
        long current = productSKUQueryRequest.getCurrent();
        long size = productSKUQueryRequest.getPageSize();
        // 查询数据库
        Page<ProductSKU> productSKUPage = productSKUService.page(new Page<>(current, size),
                productSKUService.getQueryWrapper(productSKUQueryRequest));
        return ResultUtils.success(productSKUPage);
    }

    /**
     * 分页获取商品规格列表（封装类）
     *
     * @param productSKUQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/list/page/vo")
    public BaseResponse<Page<ProductSKUVO>> listProductSKUVOByPage(@RequestBody ProductSKUQueryRequest productSKUQueryRequest,
                                                               HttpServletRequest request) {
        long current = productSKUQueryRequest.getCurrent();
        long size = productSKUQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        // 查询数据库
        Page<ProductSKU> productSKUPage = productSKUService.page(new Page<>(current, size),
                productSKUService.getQueryWrapper(productSKUQueryRequest));
        // 获取封装类
        return ResultUtils.success(productSKUService.getProductSKUVOPage(productSKUPage, request));
    }

    // endregion
}
